<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script>
		window.onload=function () {
			var oBtn=document.getElementById('btn');
			oBtn.onclick=function(){
				var xhr=null;
				if(window.XMLHttpRequest){
					xhr= new XMLHttpRequest();
				}else{
					xhr=new ActiveXObject("Microsoft.XMLHttp");
				}
				//get方式
				//& new Date()可以防止缓存
				// xhr.open("get","php/diff.php?userName=leo&"+new Date(),"true");
				// 传中文 encodeURI
				// xhr.open("get","php/diff.php?userName="+encodeURI("沈")+"&"+new Date(),"true");
				
				// post方式
				// post没有缓存问题 中文问题
				xhr.open("post","php/diff.php","true");
				//数据要放到send()
				//
				xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
				xhr.send('userName=leo&ChinaName=沈');

				xhr.onreadystatechange=function(){
					if(xhr.readyState==4){
					if(xhr.readyState==4){
						if(xhr.status==200){
							alert(xhr.responseText);
						}else{
							alert(xhr.status)
						}
					}
				}
			}
		}
	}
	</script>
</head>
<body>
	<button id="btn">获取</button>
</body>
</html>